// PowerServiceImpl.java
package org.jeecg.modules.iot.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.iot.model.power.TaiPower;
import org.jeecg.modules.iot.service.IPowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
@DS("tdengine")
public class PowerServiceImpl implements IPowerService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int add(TaiPower taiPower) { // 方法参数改为 TaiPower
        // 使用设备ID作为超级表标签
        return jdbcTemplate.update(
                "INSERT INTO  "+ taiPower.getTableName() + " USING power TAGS (?) (ts, Rpowcap, Rpow, voltage, current,ePower,soh,fPower) VALUES (?, ?, ?,  ?,?,?,?,?)",
                taiPower.getDeviceId(), taiPower.getTs(), taiPower.getRPowCap(), taiPower.getRPow(), taiPower.getVoltage(),taiPower.getCurrent(),taiPower.getEPower(),taiPower.getSoh(),taiPower.getFpower()
        );
    }


    @Override
    public TaiPower queryLastByDeviceId(String deviceId) { // 返回值类型改为 TaiPower
        try {
            String sql = "SELECT last_row(ts) ts , last_row(Rpowcap) Rpowcap, last_row(Rpow) Rpow, last_row(voltage) voltage, last_row(device_id) device_id, last_row(current) current , last_row(epower) epower , last_row(fpower) fpower FROM power WHERE device_id = ?";
            return jdbcTemplate.queryForObject(
                    sql,
                    new BeanPropertyRowMapper<>(TaiPower.class), // 使用 TaiPower 类进行映射
                    deviceId
            );
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public TaiPower queryNearestAfterTime(String deviceId, Timestamp timestamp) {
        try {
            String sql = "SELECT FIRST(ts) ts , FIRST(Rpowcap) Rpowcap, FIRST(Rpow) Rpow, FIRST(voltage) voltage, FIRST(device_id) device_id, FIRST(current) current, FIRST(epower) epower FROM power WHERE device_id = ? AND ts > ?";
            return jdbcTemplate.queryForObject(
                    sql,
                    new BeanPropertyRowMapper<>(TaiPower.class),
                    deviceId, timestamp
            );
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }


    public TaiPower queryNearestBeforeTime(String deviceId, Timestamp timestamp) {
        try {
            String sql = "SELECT last_row(ts) ts , last_row(Rpowcap) Rpowcap, last_row(Rpow) Rpow, last_row(voltage) voltage, last_row(device_id) device_id, last_row(current) current, last_row(epower) epower FROM power WHERE device_id = ? AND ts < ?";
            return jdbcTemplate.queryForObject(
                    sql,
                    new BeanPropertyRowMapper<>(TaiPower.class),
                    deviceId, timestamp
            );
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public List<TaiPower> queryByDeviceId(String deviceId) { // 返回值类型改为 List<TaiPower>
        return jdbcTemplate.query(
                "SELECT ts, Rpowcap, Rpow, voltage, device_id FROM power WHERE device_id = ?",
                new BeanPropertyRowMapper<>(TaiPower.class), // 使用 TaiPower 类进行映射
                deviceId
        );
    }

    public Page<TaiPower> queryByDeviceIdWithPagination(String deviceId, Page<TaiPower> page, String sortField, String sortOrder) {
        String countSql = "select count (*) from (select distinct  device_id from power)";
        Long total = jdbcTemplate.queryForObject(countSql, Long.class, deviceId);

        String querySql = "SELECT ts, Rpowcap, Rpow, voltage, current, epower, device_id FROM power WHERE device_id = ? ORDER BY " + sortField + " " + sortOrder + " LIMIT ? OFFSET ?";
        List<TaiPower> records = jdbcTemplate.query(
                querySql,
                new BeanPropertyRowMapper<>(TaiPower.class),
                deviceId, page.getSize(), (page.getCurrent() - 1) * page.getSize()
        );

        page.setTotal(total);
        page.setRecords(records);

        return page;
    }

    // 其他 CRUD 方法的实现...
}